from pyspark.sql import SparkSession


class SparkSessionManager:
    """
    用于管理创建和获取SparkSession实例的类
    """
    _spark_session = None

    @classmethod
    def get_spark_session(cls):
        """
        获取SparkSession实例，如果不存在则创建一个新的实例

        Returns:
            SparkSession: 返回创建好的SparkSession实例
        """
        if cls._spark_session is None:
            cls._spark_session = SparkSession.builder \
                .appName("GaussDBReadWriteExample") \
                .master("local[*]") \
                .config("spark.sql.execution.encoding", "UTF-8")\
                .config("spark.driver.extraClassPath",
                        "/opt/driver/mysql-connector-java-8.0.27.jar"
                        ":/opt/driver/gsjdbc4.jar") \
                .getOrCreate()
        return cls._spark_session
# 加载驱动的位置 根据自己的系统选择